package cn.lingnan.controller.user;


import cn.lingnan.service.UserService;
import cn.lingnan.utils.AliOSSUtils;
import cn.lingnan.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@Slf4j
@RestController
@CrossOrigin
public class UploadController {
    @Autowired
    private AliOSSUtils aliOSSUtils;

    @Autowired
    private UserService userService;

    @PostMapping("/upload")
    public Result upload(MultipartFile image,Long userId) throws IOException {
        log.info("文件上传{},用户id{}",image.getOriginalFilename(),userId);
      String url=  aliOSSUtils.upload(image);//调用工具类上传文件
      log.info("文件上传完成：{}" ,url);
        userService.updateUserImage(userId,url);//更新用户头像
      return Result.success(url);//返回路径
    }

    @PostMapping("/upload1")
    public Result upload1(MultipartFile image,Long userId) throws IOException {
        log.info("房子文件上传{},用户id{}",image.getOriginalFilename(),userId);
        String url=  aliOSSUtils.upload(image);//调用工具类上传文件
        log.info("房子文件上传完成：{}" ,url);

        return Result.success(url);//返回路径
    }
}
